
from selenium import webdriver
import time
from lxml import etree
from selenium.webdriver.common.keys import Keys
import pymysql


class douyupachong():
	db = pymysql.connect("localhost", "root", "root", "python", charset='utf8mb4', )
	# pymysql链接数据库

	def star(self,url,cursor):
		print('开始'+url)
		driver = webdriver.Chrome()
		driver.get(url)
		print(driver.title)#输出URL的标题
		#driver.page_source获取网页源代码
		html=etree.HTML(driver.page_source)
		lanmu_name=html.xpath('//*[@id="live-list-contentbox"]/li/a/p/text()')
		lanmu_url=html.xpath('//*[@id="live-list-contentbox"]/li/a/@href')
		lanmu_img=html.xpath('//*[@id="live-list-contentbox"]/li/a/img/@data-original')

		
		for lanmu_name_len in range(len(lanmu_name)):

				meta=[lanmu_name[lanmu_name_len],'https://www.douyu.com'+lanmu_url[lanmu_name_len],lanmu_img[lanmu_name_len]]
				self.leimu(meta,driver,cursor)

		self.db.close()
		driver.close()
		





	def leimu(self,meta,driver,cursor):
		driver.get(meta[1])
		try:

			html=etree.HTML(driver.page_source)
			if "'J-pager'" in driver.page_source:
				yeshu=int(html.xpath('//*[@id="J-pager"]/a/text()')[-3])
			else:
				yeshu=1	
		except Exception as e:
			logging.exception(e)
			print('该页主播只有1页')
		for zhubo_yeshu_len in range(yeshu):
			html=etree.HTML(driver.page_source)
			zhubo_biaoti=html.xpath('//*[@id="live-list-contentbox"]/li/a/@title')
			zhubo_name=html.xpath('//*[@id="live-list-contentbox"]/li/a/div/p/span[1]/text()')
			zhubo_rqi=html.xpath('//*[@id="live-list-contentbox"]/li/a/div/p/span[2]/text()')
			zhubo_id=html.xpath('//*[@id="live-list-contentbox"]/li/a/@data-rid')
			zhubo_url=html.xpath('//*[@id="live-list-contentbox"]/li/a/@href')
			for zhubo_name_len in range(len(zhubo_name)):
				try:
					if 	len(zhubo_rqi) >0:
						if '万' in zhubo_rqi[zhubo_name_len]:
							if '.' in zhubo_rqi[zhubo_name_len]:
								rqi= zhubo_rqi[zhubo_name_len][0:-1]
								zhubo_rqi[zhubo_name_len]=int(float(rqi)*10000)
							else:
								rqi=zhubo_rqi[zhubo_name_len]
								zhubo_rqi[zhubo_name_len]=int(rqi[0:-1])*10000


						mata=[zhubo_name[zhubo_name_len],zhubo_biaoti[zhubo_name_len],zhubo_rqi[zhubo_name_len],meta[0],zhubo_id[zhubo_name_len],zhubo_url[zhubo_name_len]]
						name_book=str(mata[0])
						biaoti_book=mata[1]
						rqi_book=str(mata[2])
						lanmu_book=mata[3]
						id_book=str(mata[4])
						url_book='https://www.douyu.com'+mata[5]



						sql = """INSERT INTO douyu(主播,
								 直播间名称, 人气, 游戏, 房间ID,房间链接)
								 VALUES ('%s','%s','%s','%s','%s','%s')"""% \
		   (name_book,biaoti_book,rqi_book,lanmu_book,id_book,url_book)
						print(name_book, biaoti_book, rqi_book, lanmu_book, id_book, url_book)
						cursor.execute(sql)
						self.db.commit()
						if "'J-pager'" in driver.page_source:
							lem=driver.find_element_by_css_selector('a.shark-pager-next')
							lem.send_keys(Keys.RETURN)
				except Exception as e:
					print('出现错误，更换下一个主播'+e)






	def douyusql_int(self):

		# 使用 cursor() 方法创建一个游标对象 cursor
		cursor = self.db.cursor()

		# 使用 execute() 方法执行 SQL，如果表存在则删除
		cursor.execute("DROP TABLE IF EXISTS douyu")

		#创建名为斗鱼的数据表
		sqlbiao = """CREATE TABLE douyu (
		         主播  CHAR(30) NOT NULL,
		         直播间名称  CHAR(30),
		         人气 INT ,  
		         游戏 CHAR(30),
		         房间ID INT,
		         房间链接 CHAR(200))"""
		cursor.execute(sqlbiao)
		return cursor
		print('结束————————结束')



if __name__ == "__main__":
	douyu=douyupachong()#实例化

	douyu.star('https://www.douyu.com/directory',douyu.douyusql_int())#传人url



	